<template>
  <div id="addQuestionView">
    <div>
      <h2>创建题目</h2>
    </div>
    <a-form :model="form" :style="{ width: '600px' }" label-align="left">
      <!--      field 字段的名称 label标签的提示  placeholder提示语 -->
      <a-form-item field="title" label="题目标题">
        <a-input v-model="form.title" placeholder="请输入题目的标题" />
      </a-form-item>

      <a-form-item field="tags" label="标签">
        <a-input-tag v-model="form.tags" placeholder="请输入标签" allow-clear />
      </a-form-item>

      <a-form-item field="content" label="题目内容">
        <MdEditor
          :value="form.content"
          :handle-change="onMdChange"
          style="min-width: 800px"
        />
      </a-form-item>

      <a-form-item label="判题限制" :content-flex="false" :merge-props="false">
        <a-space direction="vertical" fill style="min-width: 480px">
          <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
            <a-input-number
              mode="button"
              size="large"
              :min="0"
              v-model="form.judgeConfig.memoryLimit"
              placeholder="请输入内存限制"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.timeLimit" label="时间限制">
            <a-input-number
              mode="button"
              size="large"
              :min="0"
              v-model="form.judgeConfig.timeLimit"
              placeholder="请输入时间限制"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
            <a-input-number
              mode="button"
              size="large"
              :min="0"
              v-model="form.judgeConfig.stackLimit"
              placeholder="请输入内存限制"
            />
          </a-form-item>
        </a-space>
      </a-form-item>

      <a-form-item field="answer" label="答案">
        <MdEditor
          :value="form.answer"
          :handle-change="onCodeChange"
          style="min-width: 800px"
        />
      </a-form-item>

      <div>测试用例</div>

      <a-form-item
        v-for="(judgeCaseItem, index) of form.judgeCase"
        :label="`用例${index + 1}`"
        :key="index"
      >
        <a-space direction="vertical" fill style="min-width: 480px">
          <a-form-item
            :field="`form.judgeCase[${index}].input`"
            :label="`输入用例${index + 1}`"
            style="min-width: 480px"
          >
            <a-input
              v-model="judgeCaseItem.input"
              placeholder="请输入输入用例"
            />
          </a-form-item>
          <a-form-item
            :field="`form.judgeCase[${index}].output`"
            :label="`输出用例${index + 1}`"
            style="min-width: 480px"
          >
            <a-input
              v-model="judgeCaseItem.output"
              placeholder="请输入输出用例"
            />
          </a-form-item>
          <a-button
            @click="handleDelete(index)"
            status="danger"
            :style="{ marginLeft: '10px' }"
            >删除
          </a-button>
        </a-space>
      </a-form-item>

      <div>
        <a-button @click="handleAdd" type="outline" status="success"
          >新增
        </a-button>
      </div>
      <a-form-item>
        <a-button type="primary" @click="handleSubmit">提交</a-button>
        <!--        <a-button type="primary" @click="updateQuestion">修改</a-button>-->
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import CodeEditor from "@/components/CodeEditor.vue";
import {
  QuestionAddRequest,
  QuestionControllerService,
  QuestionView,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

//判断访问此页面为新增题目还是修改题目
const route = useRoute();
const updatePage = route.path.includes("update");
const form = ref({
  title: "",
  tags: [],
  content: "",
  judgeConfig: {
    memoryLimit: 0,
    stackLimit: 0,
    timeLimit: 0,
  },
  answer: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
});

/**
 * 根据题目id 获取题目信息
 */

const loadData = async () => {
  const id = route.query.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as any
  );
  if (res.code === 200) {
    form.value = res.data as any;
    // form.value.answer = res.data?.answer as any;
    // todo judgeConfig 展示
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig.timeLimit = Number(
        form.value.judgeConfig.timeLimit
      );
      form.value.judgeConfig.stackLimit = Number(
        form.value.judgeConfig.stackLimit
      );
      form.value.judgeConfig.memoryLimit = Number(
        form.value.judgeConfig.memoryLimit
      );
      console.log("judgeConfig", form.value.judgeConfig);
    }
    console.log("form.value: ", form.value);
  } else {
    message.error("加载失败:" + res.message);
  }
};

onMounted(() => {
  loadData();
});

//定义表达提交数据

//提交
const handleSubmit = async () => {
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 200) {
      message.success("修改成功");
    } else {
      message.error("修改失败:", res.message);
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 200) {
      message.success("创建成功");
    } else {
      message.error("创建失败:", res.message);
    }
  }
};

//标签
const handleAdd = () => {
  if (form.value.judgeCase) {
    form.value.judgeCase.push({
      input: "",
      output: "",
    });
  }
};
const handleDelete = (index: any) => {
  if (form.value.judgeCase) {
    form.value.judgeCase.splice(index, 1);
  }
};

// markdown
const onMdChange = (v: string) => {
  if (form.value.title) {
    form.value.title = v;
  }
};

// codeEditor
const onCodeChange = (v: string) => {
  if (form.value.answer) {
    form.value.answer = v;
  }
};
</script>

<style scoped>
#addQuestionView {
}
</style>
